package com.ruoyi.service.impl;

import java.util.List;

import com.ruoyi.pojo.vo.UnreadCountVo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mapper.UnreadCountMapper;
import com.ruoyi.domain.UnreadCount;
import com.ruoyi.service.IUnreadCountService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 未读消息数Service业务层处理
 *
 * @author qyb
 * @date 2023-12-26
 */
@Service
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class UnreadCountServiceImpl implements IUnreadCountService {
    private final Long ADMIN = 1l;//管理员账户uuid关联未读消息表

    private final Long ADMIN_ROLE = 1l;//管理员角色
    private final Long TEACHER_ROLE = 3l;//老师角色
    private final UnreadCountMapper unreadCountMapper;
    private final SysUserRoleMapper sysUserRoleMapper;


    @Override
    public AjaxResult getUnReadCount() {
        Long userId = SecurityUtils.getUserId();
        UnreadCountVo countVo = new UnreadCountVo();
        countVo.setUsername(SecurityUtils.getUsername());
        countVo.setUserId(userId);
        if (!isAdmin(userId)) {
            UnreadCount un = unreadCountMapper.getUnreadCountByUserId(userId);
            countVo.setCount(un != null ? un.getUnreadCount() : 0);
        } else {
            UnreadCount un = unreadCountMapper.getUnreadCountByUserId(ADMIN);
            countVo.setCount(un != null ? un.getUnreadCount() : 0);
        }
        return AjaxResult.success(countVo);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean addCount(Long userId, int type) {
        //管理员未读消息
        if (type == 2) {
            return addCount(ADMIN);
        }
        //普通用户未读消息
        if (type == 1) {
            return addCount(userId);
        }
        return false;
    }

    private Boolean addCount(Long userId) {
        UnreadCount unreadCount = unreadCountMapper.getUnreadCountByUserId(userId);
        if (unreadCount == null) {
            UnreadCount count = new UnreadCount();
            count.setUserId(userId);
            count.setUnreadCount(1l);
            return unreadCountMapper.insertUnreadCount(count) == 1;
        } else {
            Long nubmer = unreadCount.getUnreadCount() + 1;
            unreadCount.setUnreadCount(nubmer);
            return unreadCountMapper.updateUnreadCount(unreadCount) == 1;
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean clearCount(Long userId, int type) {
        //用户已读消息
        if (type == 1) {
            return clearCount(userId);
        }
        //管理员已读消息
        if (type == 2) {
            return clearCount(ADMIN);
        }
        return false;

    }

    private Boolean clearCount(Long userId) {
        UnreadCount unreadCount = unreadCountMapper.getUnreadCountByUserId(userId);
        while (unreadCount != null) {
            //对所有用户未读消息减一
            unreadCount.setUnreadCount(unreadCount.getUnreadCount() > 1 ? unreadCount.getUnreadCount() - 1 : 0);
            return unreadCountMapper.updateUnreadCount(unreadCount) == 1;
        }
        return false;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean clearAllCount(Long userId) {
        //对用户未读消息清零
        if (!isAdmin(userId)) {

            UnreadCount unreadCount = unreadCountMapper.getUnreadCountByUserId(userId);
            while (unreadCount != null) {
                unreadCount.setUnreadCount(0l);
                return unreadCountMapper.updateUnreadCount(unreadCount) == 1;
            }

        }
        //管理员未读消息清零
        else {
            UnreadCount unreadCount = unreadCountMapper.getUnreadCountByUserId(ADMIN);
            while (unreadCount != null) {
                //对管理员未读消息清零
                unreadCount.setUnreadCount(0l);
                return unreadCountMapper.updateUnreadCount(unreadCount) == 1;
            }


        }
        return false;
    }


    private boolean isAdmin(Long userId) {
        SysUserRole roleByUserId = sysUserRoleMapper.getRoleByUserId(userId);
        if (roleByUserId == null) {
            return false;
        }
        if (roleByUserId.getRoleId() == ADMIN_ROLE||roleByUserId.getRoleId()==TEACHER_ROLE) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 查询未读消息数
     *
     * @param uuid 未读消息数主键
     * @return 未读消息数
     */
    @Override
    public UnreadCount selectUnreadCountByUuid(Long uuid) {
        return unreadCountMapper.selectUnreadCountByUuid(uuid);
    }

    /**
     * 查询未读消息数列表
     *
     * @param unreadCount 未读消息数
     * @return 未读消息数
     */
    @Override
    public List<UnreadCount> selectUnreadCountList(UnreadCount unreadCount) {
        return unreadCountMapper.selectUnreadCountList(unreadCount);
    }

    /**
     * 新增未读消息数
     *
     * @param unreadCount 未读消息数
     * @return 结果
     */
    @Override
    public int insertUnreadCount(UnreadCount unreadCount) {
        return unreadCountMapper.insertUnreadCount(unreadCount);
    }

    /**
     * 修改未读消息数
     *
     * @param unreadCount 未读消息数
     * @return 结果
     */
    @Override
    public int updateUnreadCount(UnreadCount unreadCount) {
        return unreadCountMapper.updateUnreadCount(unreadCount);
    }

    /**
     * 批量删除未读消息数
     *
     * @param uuids 需要删除的未读消息数主键
     * @return 结果
     */
    @Override
    public int deleteUnreadCountByUuids(Long[] uuids) {
        return unreadCountMapper.deleteUnreadCountByUuids(uuids);
    }

    /**
     * 删除未读消息数信息
     *
     * @param uuid 未读消息数主键
     * @return 结果
     */
    @Override
    public int deleteUnreadCountByUuid(Long uuid) {
        return unreadCountMapper.deleteUnreadCountByUuid(uuid);
    }
}
